package com.ngc.mall.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ngc.mall.common.configure.mybatisPlus.BaseServiceImpl;
import com.ngc.mall.domain.SpuDomain;
import com.ngc.mall.domain.ThemeDomain;
import com.ngc.mall.mapper.ThemeMapper;
import com.ngc.mall.service.IThemeService;
import com.ngc.mall.vo.ThemeVO;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ThemeServiceImpl extends BaseServiceImpl<ThemeMapper, ThemeDomain> implements IThemeService {
    @Override
    public Long createTheme(ThemeDomain themeDomain) {
        return super.create(themeDomain);
    }

    @Override
    public Boolean updateThemeById(Long id,ThemeDomain themeDomain) {

        return super.update(Wrappers.<ThemeDomain>lambdaUpdate()
                .eq(ThemeDomain::getId,id)
                .set(ThemeDomain::getDescription,themeDomain.getDescription())
                .set(ThemeDomain::getEntranceImg,themeDomain.getEntranceImg())
                .set(ThemeDomain::getInternalTopImg,themeDomain.getInternalTopImg())
                .set(ThemeDomain::getSort,themeDomain.getSort())
                .set(ThemeDomain::getIsEnabled,themeDomain.getIsEnabled())
        );
    }

    @Override
    public Boolean updateStatusById(Long id, boolean isEnabled) {
        return super.update(Wrappers.<ThemeDomain>lambdaUpdate()
                .eq(ThemeDomain::getId,id)
                .set(ThemeDomain::getIsEnabled,isEnabled)
        );
    }

    @Override
    public ThemeVO getDetailById(Long id) {
        ThemeDomain domain=super.getById(id);
        List<SpuDomain> spuDomainList =baseMapper.findThemeWithSpus(id);
        ThemeVO themeVO= ThemeVO.builder().description(domain.getDescription()).entranceImg(domain.getEntranceImg()).name(domain.getName()).spuList(spuDomainList).internalTopImg(domain.getInternalTopImg()).isEnabled(domain.getIsEnabled()).sort(domain.getSort()).id(domain.getId()).build();
        return themeVO;
    }

    @Override
    public void insertBatchSpuTheme(Long spuId, List<Long> themeIdList) {
        System.out.println(spuId);
        System.out.println(themeIdList);
       Boolean result=baseMapper.insertBatchSpuTheme(spuId,themeIdList);
       if(!result){
           System.out.println("插入spu_theme失败");
//           throw new BusinessException();
       }
    }



    @Override
    public List<Long> findIdBySpus(Long spuId){
        return baseMapper.findIdBySpus(spuId);
    }

    @Override
    public Boolean deleteSpuById(Long id) {
        return baseMapper.removeSpuIdById(id);
    }
}
